<?php 
session_start();
/*$link = mysql_connect ( 'localhost', 'root', 'libraryred1' );
if (! $link) {
	die ( 'Could not connect: ' . mysql_error () );
}*/
//print_r($_SESSION);
$ss = $_COOKIE['SAMSsession'];

	$sid = $_COOKIE['HS7BDF'];

	$name =substr($sid,0,stripos($sid,"\\"));

	$temp = substr($ss,40);

	$temp1=explode(".",$temp);

	$temp1=array_unique($temp1);

	$oucu=$temp1[0];

	$ssid=$temp1[1];

	$isstudent=true;

	if($_SERVER['HTTP_SAMS_STUDENTPI']=='00000000')$isstudent=false;

	if (!$ssid) $ssid=$temp1[2];
	$_SESSION['oucu']= $_SERVER['HTTP_SAMS_USER'];
	$_SESSION['ssid']=$_SERVER['HTTP_SAMS_STAFFID'];
	$_SESSION['spi']=$_SERVER['HTTP_SAMS_STUDENTPI'];
	$_SESSION['stid']=$_SERVER['HTTP_SAMS_TUTORID'];
	$_SESSION['svid']=$_SERVER['HTTP_SAMS_VISITORID'];
	$_SESSION['sname']=$_COOKIE['HS7BDF'];
	$_SESSION['isstudent'] =$isstudent;
	$_SESSION['samstimecheck']=mktime();
	
	$tempname = explode(" ",$_SESSION['sname']);
	$surname = $tempname[count($tempname)-1];
	
	if ($isstudent) $id = $_SERVER['HTTP_SAMS_STUDENTPI'];
	if ($id !='00000000') $id = $_SERVER['HTTP_SAMS_STAFFID'];
	$authrequest ='<ser:serviceParameters xmlns:ser="http://www.endinfosys.com/Voyager/serviceParameters">
   <ser:parameters>
      <ser:parameter key="pin">
         <ser:value></ser:value>
      </ser:parameter>
   </ser:parameters>
      <ser:patronIdentifier lastName="'.$surname.'">
      <ser:authFactor type="I">'.$id.'</ser:authFactor>
   </ser:patronIdentifier>
</ser:serviceParameters>
	';
	//echo $id.$authrequest;
	
	
$url='http://bulbul.open.ac.uk:7014/vxws/AuthenticatePatronService';

       $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 4); 

       //curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:text/xml"));
       //curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Accept:text/xml"));

  
       curl_setopt($ch, CURLOPT_POSTFIELDS, $authrequest);
       //curl_setopt($ch, CURLOPT_POST,1); 
       


       $r=curl_exec($ch); 
if (curl_errno($ch)) { 
           print curl_error($ch); 
       } else { 
           curl_close($ch); 
       } 
if (strstr($r,'Patron not valid')) {header ("location:http://css2.open.ac.uk/VoyagerRegistration/SelfRegistrationPage/Registration.aspx");
die();}
//echo "!";
$xml = ereg_replace(":","",$r);
$xml = simplexml_load_string($xml);
//print_r($xml);
foreach($xml->serserviceData->patpatronIdentifier->attributes() as $a => $b)
{
	$_SESSION[$a]=trim($b);
	
}
//print_r($xml);
//	echo "<pre>";
$loginurl ="http://voyager.open.ac.uk/vwebv/login.do?loginType=B&loginId={$xml->serserviceData->patbarcode}&lastName={$xml->serserviceData->patlastName}";
if (!isset($_GET["plugin"]))
{header("location:http://voyager.open.ac.uk/vwebv/login.do?loginType=B&loginId={$xml->serserviceData->patbarcode}&lastName={$xml->serserviceData->patlastName}");
die(0);	
	
}
	
$url = "http://bulbul.open.ac.uk:7014/vxws/MyAccountService?patronId={$_SESSION['patronId']}&patronHomeUbId={$_SESSION['patronHomeUbId']}";
//$url ="http://www.google.com";
//echo file_get_contents($url);
//echo $query;
//$res = mysql_query($query);

//if(!mysql_num_rows($res))
{
	$ch = curl_init($url);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	//curl_setopt($ch, CURLOPT_PROXY, "http://wwwcache.open.ac.uk:80");
	//curl_setopt($ch, CURLOPT_PROXYPORT, 80);


	$x = curl_exec($ch);	
	
}	

$x = ereg_replace(":","",$x);
$xml = simplexml_load_string($x);
print "<h1>Current loans</h1><hr>";
foreach ($xml->serserviceData->myacchargedItems->myacclusterChargedItems->myacchargedItem as $item){
	
	print $item->myactitle."<br>";
	print $item->myacauthor."<br>";
	print date("d/M/Y",strtotime(substr($item->myacdueDate,0,10)))."<br>";
	print "<hr>";
}


$url= $loginurl; 
echo "<a href='$url'>$url</a>";
echo "<pre>";
print_r($xml);
echo "</pre>";
	
$url = "http://bulbul.open.ac.uk:7014/vxws/patron/{$_SESSION['patronId']}/patronInformation/address?patronHomeUbId={$_SESSION['patronHomeUbId']}";
//$url ="http://www.google.com";

//echo file_get_contents($url);
//echo $query;
$res = mysql_query($query);

//if(!mysql_num_rows($res))
{
	$ch = curl_init($url);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	//curl_setopt($ch, CURLOPT_PROXY, "http://wwwcache.open.ac.uk:80");
	//curl_setopt($ch, CURLOPT_PROXYPORT, 80);


	$x = curl_exec($ch);	
	
}	
	

	
	
?>



